﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;

namespace CECB.LeopoldMetrixCharts
{
    /// <summary>
    /// Interaction logic for LeopoldMetrixCharts_Window1.xaml
    /// </summary>
    public partial class LeopoldMetrixCharts_Window1 : Window
    {
        public LeopoldMetrixCharts_Window1()
        {
            InitializeComponent();
            
        }
        public LeopoldMetrixCharts_Window1(string prj )
        {
            InitializeComponent();
            showColumnChart(prj );
        }
        DSSEntities cecbContext = new DSSEntities();

        double sum = 0;
        private void showColumnChart(String prj )
        {
            /// get summation //

            var quary = (from c in cecbContext.LeopoldMatrixValues

                         where c.proj_reference.Contains(prj)



                         group c by new
                         {
                             c.proj_reference
                         }
                             into cc
                             select cc.Sum(c => c.mtrx_importance*c.mtrx_magnitude)).FirstOrDefault();
            sum = quary.Value;



            double sociologicalEffection = 0;
            double PhysiologicalAffection = 0;
            double EchologicalAffection = 0;
             
            double isCorrect = 0;

            try
            {
                var quary1 = (from c in cecbContext.LeopoldMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(prj)
                             && g.cmpt_mainCat.Contains("Social Environmental")

                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_importance*c.mtrx_magnitude) / sum).FirstOrDefault();

                sociologicalEffection = quary1.Value;
            }
            catch (Exception)
            {
                isCorrect = -1;

            }
            sociologicalEffection = Math.Round(sociologicalEffection, 2) * 100;

         /////////////// phisycal Environment //////////////////////////////
            double physiologicalAffection = 0;

            try
            {
                var quary1 = (from c in cecbContext.LeopoldMatrixValues
                              join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                              join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                              where c.proj_reference.Contains(prj)
                              && g.cmpt_mainCat.Contains("Physical Environment")

                              group c by new
                              {
                                  c.proj_reference
                              }

                                  into cc
                                  select cc.Sum(c => c.mtrx_importance*c.mtrx_magnitude) / sum).FirstOrDefault();

                physiologicalAffection = quary1.Value;
            }
            catch (Exception)
            {
                isCorrect = -1;

            }
            physiologicalAffection = Math.Round(physiologicalAffection, 2) * 100;



        //////////////////////////////////////////////////////////////////////////////////
            

            try
            {
                var quary1 = (from c in cecbContext.LeopoldMatrixValues
                              join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                              join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                              where c.proj_reference.Contains(prj)
                              && g.cmpt_mainCat.Contains("Ecological Environmental")

                              group c by new
                              {
                                  c.proj_reference
                              }

                                  into cc
                                  select cc.Sum(c => c.mtrx_importance*c.mtrx_magnitude) / sum).FirstOrDefault();

                EchologicalAffection = quary1.Value;
            }
            catch (Exception)
            {
                isCorrect = -1;

            }
            EchologicalAffection = Math.Round(EchologicalAffection, 2) * 100;



            //////////////////////////////////////////////////////////////////////////////////////
       




 


            List<KeyValuePair<string, double>> valueList = new List<KeyValuePair<string, double>>();
            valueList.Add(new KeyValuePair<string, double>("Physical Environment", physiologicalAffection));
            valueList.Add(new KeyValuePair<string, double>("Social Environmental", sociologicalEffection));
            valueList.Add(new KeyValuePair<string, double>("Ecological Environmental", EchologicalAffection));

            //Setting data for column chart
            columnChart.DataContext = valueList;

            // Setting data for pie chart
            pieChart.DataContext = valueList;

            //Setting data for area chart
            areaChart.DataContext = valueList;

            //Setting data for bar chart
            barChart.DataContext = valueList;

            //Setting data for line chart
            lineChart.DataContext = valueList;
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }


    }
}
